package com.kylin.common.security.handler;

import com.kylin.common.core.beans.GlobalResultCode;
import com.kylin.common.core.beans.ResultBean;
import com.kylin.common.security.domain.LoginUser;
import com.kylin.common.security.util.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

/**
 * 退出成功处理器
 * @author wuhao
 * @version 1.0 - 2023/1/13
 */
@Slf4j
@Component
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)  {
        LoginUser user = (LoginUser) authentication.getPrincipal();
        String username = user.getUsername();
        log.info("username: {}  is offline now", username);

        ResultBean<Serializable> resultBean = new ResultBean<>(GlobalResultCode.SUCCESS);
        ResponseUtils.out(response, resultBean);
    }
}
